Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

E0277: suggest dereferencing function arguments in more cases #133292

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

dianne
Copy link
Contributor

@dianne dianne commented Nov 21, 2024

This unifies and generalizes some of the logic in TypeErrCtxt::suggest_dereferences so that it will suggest dereferencing arguments to function/method calls in order to satisfy trait bounds in more cases.

Previously it would only fire on reference types, and it had two separate cases (one specifically to get through custom Deref impls when passing by-reference, and one specifically to catch #87437). I've based the new checks loosely on what's done for E0308 in FnCtxt::suggest_deref_or_ref: it will suggest dereferences to satisfy trait bounds whenever the referent is Copy, is boxed (& so can be moved out of the boxes), or is being passed by reference.

This doesn't make the suggestion fire in contexts other than function arguments or binary operators (which are in a separate case that this doesn't touch), and doesn't make it suggest a combination of &-removal and dereferences. Those would require a bit more restructuring, so I figured just doing this would be a decent first step.

Closes #90997

@rustbot
Copy link
Collaborator

rustbot commented Nov 21, 2024

r? @fee1-dead

rustbot has assigned @fee1-dead.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 21, 2024
// reference, `ty` is `Copy`, or we're moving out of a (potentially nested) `Box`.
let can_deref = is_under_ref.is_some()
|| self.type_is_copy_modulo_regions(obligation.param_env, ty)
|| ty.is_numeric() // for inference vars (presumably but not provably `Copy`)
Copy link
Contributor Author

@dianne dianne Nov 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ty.is_numeric() case is admittedly a hack for tests/ui/traits/suggest-dereferences/suggest-dereferencing-receiver-argument.rs, since it wants a deref suggestion for an &{integer}. I think a slightly more principled (still hacky) way to do this could be to walk through base_ty and add Copy bounds to the ParamEnv for any numeric inference vars. It should maybe also downgrade the suggestion to MaybeIncorrect if any type inference vars are present. {integer}: Sized does appear to be provable already, at least.

@fee1-dead
Copy link
Member

r? compiler

@rustbot rustbot assigned pnkfelix and unassigned fee1-dead Nov 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Trait errors should suggest * if appropriate for the boudn to succeed
4 participants